package com.hzw.net.net.json

import com.google.gson.*
import java.lang.reflect.Type

/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2020/4/15 16:27
 *
 * @Description:    Double类型字段解析适配
 *
 */
class DoubleAdapter : JsonSerializer<Double>, JsonDeserializer<Double> {
    override fun serialize(
        src: Double?,
        typeOfSrc: Type?,
        context: JsonSerializationContext?
    ): JsonElement = JsonPrimitive(src)

    override fun deserialize(
        json: JsonElement?,
        typeOfT: Type?,
        context: JsonDeserializationContext?
    ): Double {
        if (json == null) {
            return 0.0
        }
        if (json.asString == "" || json.asString == null || json.asString == "null") {
            return 0.0
        }

        return try {
            json.asDouble
        } catch (e: Exception) {
//            L.e( "Double类型字段解析错误 ${e.message}", e)
            0.0
        }
    }
}